<?php
/**
 * 《联系信息》实用类
 *
 * @author 孙晓晔
 * @version $Id: ExpressUtil.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class ContactUtil extends AdminUtil {
	public $way = array(
		'-' => '请选择',
		'1' => '快递',
		'2' => '海运',
		'3' => '空运',
		'9' => '其它',
	);
	
	public $cert_type = array(
		'-' => '请选择',
		'1' => '组织机构代码',
		'2' => '营业执照',
		'3' => '身份证',
		'4' => '注册证',

		'9' => '其它',
	);
	
	public $dept = array(
		'-' => '请选择',
		'销售部' => '销售部',
		'市场部' => '市场部',
		'业务部' => '业务部',
		'工程部' => '工程部',
		'财务部' => '财务部',
		'人事部' => '人事部',
		'行政部' => '行政部',
		'制造部' => '制造部',
			
		'其它' => '其它',
	);
	
	public $job = array(
		'-' => '请选择',
		'主管' => '主管',
		'经理' => '经理',
		'总监' => '总监',
		'副总' => '副总',
		'董事' => '董事',
			
		'业务员' => '业务员',
		'工程师' => '工程师',
		'设计师' => '设计师',
		
		'总经理' => '总经理',
		'董事长' => '董事长',
			
		'其它' => '其它',
	);
		
	function __construct() {
		$request['update'] = '$Date: 2009/03/03 07:24:42 $';
		$request['revision'] = '$Revision: 1.5 $';
			
		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('cert_type_option', $this->cert_type);
		$controller->assign_by_ref('way_option', $this->way);
		$controller->assign_by_ref('job_option', $this->job);
		$controller->assign_by_ref('dept_option', $this->dept);
	}
	
	function _update_status_x($from, $to, $createtime = '-') {
		$db = newdb($this->dsn_w);
		if (empty($createtime) || $createtime == '-') {
			$fmt = "UPDATE %s SET status = '%s' WHERE status = '%s'";
			$sql = sprintf($fmt, $this->tbl, $to, $from);
		} else {
			$fmt = "UPDATE %s SET status = '%s' WHERE createtime < '%s' AND status = '%s'";
			$sql = sprintf($fmt, $this->tbl, $to, $createtime, $from);
		}
		
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function _update_status($id, $status, $createtime = '-') {
		$db = newdb($this->dsn_w);
		if (empty($createtime) || $createtime == '-') {
			$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'" ;
			$sql = sprintf($fmt, $this->tbl, $status, $id);
		} else {
			$fmt = "UPDATE %s SET status = '%s' WHERE createtime < '%s' AND id = '%s'" ;
			$sql = sprintf($fmt, $this->tbl, $status, $createtime, $id);
		}
		echo $sql . "\n";
		$db->query($sql);
	}

	function time($controller) {
		$hour_option = array();
		for ($i = 0; $i < 41; $i++) {
			$hour_option[$i] = $i . "小时";
		}

		$minute_option = array();
		for ($i = 0; $i < 60; $i++) {
			if ($i % 5 > 0) {
				continue;
			}
			$minute_option[$i] = $i . "分钟";
		}

		$controller->assign_by_ref('hour_option', $hour_option);
		$controller->assign_by_ref('minute_option', $minute_option);

	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 更新状态
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
}
